package services.AB01;

import java.text.SimpleDateFormat;
import java.util.*;
import com.*;
import com.util.Util;
import vo.system.AB01;
import vo.system.AB01_LS;
import java.util.Date;
/**
 * PersonMgrFacade：单位信息管理services
 * @author beijunhua
 * @version Sun April 10 17:01:57 CST 2016 zhd
 * @author ($Date$ modification by $Author$)
 * @since 1.0
 */ 
public class AB01DWServices {
	PublicSystem sys = PublicSystem.getInstance();
	/**
	 * 所有单位包括注销信息记录
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnit(Object[] obj,HashMap parms) throws Exception {
		String sql = "select * from v_ab01 where ('"+obj[0]+"' is null or aab001=? )  and" +
				     " ('"+obj[1]+"' is null or aab004 like '%'||?||'%') and ('"+obj[2]+"' is null or aab620 like '%'||?||'%' )" ;
		Map map = new HashMap();
		/*调用统一表单方法并返回map*/
		return sys.getGrid(sql, obj, parms, false);
	}
	/**
	 * 所有单位不包括注销信息记录
	 * @param obj
	 * @param parms
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnit1(Object[] obj,HashMap parms) throws Exception {
		String sql = "select * from v_ab01 where aab620= '正常' and ('"+obj[0]+"' is null or aab001=? )  and" +
				" ('"+obj[1]+"' is null or aab004 like '%'||?||'%') " ;
		Map map = new HashMap();
		/*调用统一表单方法并返回map*/
		return sys.getGrid(sql, obj, parms, false);
	}
	/**
	 * 选中该条记录的单位的基本信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitData (String id) throws Exception {
		AB01 vo = new AB01();
		String sql = "select * from ab01 where AAB001 = ? and aab620='0' ";	
		List data = sys.query(sql, new Object[] {id}, true);		
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	} 
	/**
	 * 选中该条记录的单位的基本信息检测
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitData1 (String id) throws Exception {
		AB01 vo = new AB01();
		String sql = "select * from ab01 where AAB001 = ?  ";	
		List data = sys.query(sql, new Object[] {id}, true);		
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	} 
	/**
	 * 判断单位名是否重复
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitName (String name) throws Exception {
		String sql = "select * from ab01 where AAB004 = ? ";
		List data = sys.query(sql, new Object[] {name}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	} 
	/**
	 * 判断统一社会信用编码是否重复
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitCreditId (String id) throws Exception {
		String sql = "select * from ab01 where AAB998 = ?";
		List data = sys.query(sql, new Object[] {id}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	} 
	/**
	 * 编辑界面组织机构id判断是否重复 
	 * @param id
	 * @param sb
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitOrganizationId (String id, String sb) throws Exception {
		String sql = "select * from ab01 where AAB003 = ? and AAB001 <> ?";
		List data = sys.query(sql, new Object[] {id,sb}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	}
	/**
	 * 新增界面判断组织机构id是否重复
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HashMap getUnitOrganizationIds (String id) throws Exception {
		String sql = "select * from ab01 where AAB003 = ? ";
		List data = sys.query(sql, new Object[] {id}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	}
	/**
	 * 修改表原始数据，并将信息插入历史记录表
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String modifiedUnit(HashMap obj) throws Exception {
	 	Date now = new Date(); 		
		AB01 vo = new AB01();		
		vo = (AB01)Util.MapToVo(vo, obj);
		vo.setAAE036(now);
		if(vo.getAAB620() == null) {
			vo.setAAB620("0");
		}		
		sys.Jdbc_modifyDao(vo, new Object[]{"aab001:A"});
		return "";
	}
	/**
	 * 新增表原始数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String checkData(HashMap obj) throws Exception {
	 	Date now = new Date(); 
		AB01 vo = new AB01();
		Map map = new HashMap();
		map.put("inValueStr", obj.get("aab998"));
		map.put("outNumber", "0");
		String strtt = (String) obj.get("aab998");
		String str1 = sys.callDBFuncProce("zy_set_ab01.chk_insert_ab01", "function", obj);
		String[] str2 = str1.split("&") ;
		if(str2 == null || str2.length == 0) {
			return "";
		} else {
			Map returnmap1 = jxStringToMap(str2[1]);
			if(null != returnmap1.get("hint1") && !"".equals(returnmap1.get("hint1")) && !"null".equals(returnmap1.get("hint1"))) {
				return "hint1" + (String) returnmap1.get("hint1");
			}
			if(null != returnmap1.get("hint2") && !"".equals(returnmap1.get("hint2")) && !"null".equals(returnmap1.get("hint2"))) {
			  return (String) returnmap1.get("hint2");
			}
		}
		return str2[0];	
	} 
	/**
	 * 新增表原始数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String updateData(HashMap obj) throws Exception {
	 	Date now = new Date(); 
		AB01 vo = new AB01();
		Map map = new HashMap();
		String str1 = sys.callDBFuncProce("zy_set_ab01.set_update_ab01", "function", obj);
		String[] str2 = str1.split("&") ;
		if(str2 == null || str2.length == 0) {
			return "error";
		}
		else {
			Map returnmap1 = jxStringToMap(str2[1]);
			return str2[0];
		}
	} 
	
	/**
	 * 新增表原始数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String cancel(HashMap obj) throws Exception {
	 	Date now = new Date(); 
		AB01 vo = new AB01();
		Map map = new HashMap();
		String str1 = sys.callDBFuncProce("zy_set_ab01.set_ab01_aab602", "function", obj);
		String[] str2 = str1.split("&") ;
		if(str2 == null || str2.length == 0) {
			return "error";
		} else {
			Map returnmap1 = jxStringToMap(str2[1]);
			if(null != returnmap1.get("hint1") && !"".equals(returnmap1.get("hint1")) && !"null".equals(returnmap1.get("hint1"))) {
			  return "hint1" + (String) returnmap1.get("hint1");
			}
			if(null != returnmap1.get("hint2") && !"".equals(returnmap1.get("hint2")) && !"null".equals(returnmap1.get("hint2"))) {
			  return (String) returnmap1.get("hint2");
			}
		}
		return str2[0];	
	} 
	/**
	 * 新增表原始数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String insertUnit(HashMap obj) throws Exception {
	 	Date now = new Date(); 
		AB01 vo = new AB01();
		Map map = new HashMap();
		map.put("inValueStr", obj.get("aab998"));
		map.put("outNumber", "0");
		String str1 = sys.callDBFuncProce("zy_set_ab01.chk_insert_ab01", "function", obj);
		String[] str2 = str1.split("&") ;
		if(str2 == null || str2.length == 0) {
			return "error";
		} else {
			String strtt = (String) obj.get("aab998");
			if(!"".equals(strtt)) {
					if(strtt.length() != 18) {
						return "统一社会信用代码为18位";
					}
			}
			Map returnmap1 = jxStringToMap(str2[1]);
			if(null != returnmap1.get("hint1") && !"".equals(returnmap1.get("hint1")) && !"null".equals(returnmap1.get("hint1"))) {
			  return "hint1" + (String) returnmap1.get("hint1");
			}
			if(null != returnmap1.get("hint2") && !"".equals(returnmap1.get("hint2")) && !"null".equals(returnmap1.get("hint2"))) {
			  return (String) returnmap1.get("hint2");
			}
		}
		if(!"0".equals(str2[0])) {
			return str2[0];	
		}
	 	String ss = sys.callDBFuncProce("zy_set_ab01.insert_ab01", "function", obj);
	 	String[] str3 = ss.split("&") ;
	     if (!"0".equals(str3[0])) {
	     	return str3[0];
	     } else {
	     	Map returnmap = jxStringToMap(str3[1]);
	     	String sql = "update ab01 set aab620='0' where aab001='"+returnmap.get("aab001")+"'";
	     	sys.executeSql(sql);
	     	return (String) returnmap.get("aab001");	
	     }
	} 
	/**
	 * 新增表原始数据
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String insertUnit1(HashMap obj) throws Exception {
	 	Date now = new Date(); 
		AB01 vo = new AB01();
		Map map = new HashMap();
		String ss = sys.callDBFuncProce("zy_set_ab01.insert_ab01", "function", obj);
	 	String[] str3 = ss.split("&") ;
	    if (!"0".equals(str3[0])) {
	     		return str3[0];
	    } else {
	     		Map returnmap = jxStringToMap(str3[1]);
               String sql = "update ab01 set aab620='0' where aab001='" + returnmap.get("aab001") + "'";
	     		sys.executeSql(sql);
	     		return (String) returnmap.get("aab001");	
	     	}
	} 
	/**
	 * 下拉框选项
	 * @param lx
	 * @return
	 * @throws Exception
	 */
	public List getSelect(String lx) throws Exception {
		String sql = "select aaa103,aaa102 FROM aa10 where aaa100=?";
		List data = sys.query(sql, new Object[] {lx}, true);
		return data;
	}
	public List getSelect1(String lx) throws Exception {
		String sql = "select aaa103,aaa102 FROM aa10 where aaa100=? and aaa102 !='0'";
		List data = sys.query(sql, new Object[] {lx}, true);
		return data;
	}
	 /**
	  * 校验身份证
	  * @param obj
	  * @return
	  * @throws Exception
	  */
	public String checksfz(HashMap obj) throws Exception{
		String ss = sys.callDBFuncProce("zj_gf.chk_shbzh", "function", obj).toString();		
		String[] result = ss.split("&") ;
		if(result[0].equals("0")) {
			return "0";
		}
		else {	
			String[] str =result[0].split(":");
			return str[0];
		}
	 }
	/**
	 * 查询原数据库中所选id的数据
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public HashMap getAb01Datas(String id) throws Exception {
		String sql = "select aab004,aab019,aae004,aae005,aab013,aae018,aab014,aab015 from ab01 where aab001=?";
		List data = sys.query(sql, new Object[] {id}, true);
		return data.size() > 0 ? (HashMap)data.get(0) : null;
	} 
	/**
	 * 查询单条记录的某字段值
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public Map jxStringToMap(String str) throws Exception {
		Map map = new HashMap();
		String str1 = sys.SelResultStr("select chr(1) from dual");
	    String str2 = sys.SelResultStr("select chr(2) from dual");
	    if(str == null || "".equals(str.trim()) || "null".equals(str)) {
	    	return map;
	    }
	    String[] data = str.split(str1);
	    if(data!= null && data.length > 0) {
	    	for(int i = 0 ; i < data.length; i ++) {
	    		if(data[i] == null || "".equals(data[i].trim()) || "null".equals(data[i])) {
			    	continue ;
			    }
	    		String[] data1 = data[i].split(str2);
	    		if(data1.length == 1) {
	    			map.put(data1[0], "");
	    		}
	    		else {
	    			map.put(data1[0], data1[1]);
	    		}
	    	}
	    }
		return map;
	}
}

